package day01;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 */
public class JsonObjectTest {
    public static void main(String[] args) {
        JSONObject object1 = new JSONObject();
        object1.put("name","张三");
        object1.put("name1","张珊");
        object1.put("name2","章三");
        String name = (String) object1.get("name");
        System.out.println(name);
        int size = object1.size();
        System.out.println(size);
        boolean result = object1.isEmpty();
        System.out.println(result);
        boolean isContainsKeyResult = object1.containsKey("name");
        System.out.println(isContainsKeyResult);
        boolean isContainsValueResult = object1.containsValue("33");
        System.out.println(isContainsValueResult);
        /**
         * 如果JSONObject对象中的value是一个JSONObject对象，根据key获取对应的JSONObject对象
         */
        JSONObject object2 = new JSONObject();
        object2.put("student1",object1);
        JSONObject student = object2.getJSONObject("student1");
        System.out.println(student);
        /**
         * 如果JSONObject对象中的value是一个JSONObject数组，格局key获取对应的JSONObject数组
         */
        JSONObject objectArray = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(0,"下标为0的元素");
        jsonArray.add(1,"下标为1的元素");
        jsonArray.add(2,"下标为2的元素");
        objectArray.put("testArray",jsonArray);
        /**获取JSONObject对象中的JSONArray数组*/
        JSONArray jsonArray2 = objectArray.getJSONArray("testArray");
        System.out.println(jsonArray2);
        /**根据key移除JSONObject对象中的某个键值对*/
        object1.remove("name");
        System.out.println(object1);
        /**取得JSONObject对象中的集合*/
        Set<String> keySet = object1.keySet();
        for (String key : keySet) {
            System.out.println("\t"+key);
        }
        /**取得JSONObject对象中的键和值的映射关系*/
        Set<Map.Entry<String,Object>> entrySet = object1.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            System.out.println(entry);
        }
        /**转化为json字符串*/
        String str1 = object1.toJSONString();
        System.out.println(str1);
        String str2 = object1.toString();
        System.out.println(str2);
    }

}